<!-- eslint-disable vue/multi-word-component-names -->
<script setup lang="ts">
import { onMounted, onUpdated, ref } from 'vue'
import { getUserInfo } from '@/api/user'
import { ElNotification } from 'element-plus'

const username = ref('')

onMounted(() => {
  getUserInfo()
    .then((res) => {
      // console.log(res)
      username.value = res.data
    })
    .catch((err) => {
      ElNotification({
        title: 'Error',
        message: err.messsage,
        duration: 1000,
        type: 'error'
      })
      console.log('获取用户信息错误：', err.messsage)
    })
})

import { useHeaderStore } from '@/stores/modudels/headerStore'
import { useUserStore } from '@/stores/modudels/userStores'
import { useRouter, useRoute } from 'vue-router'

const router = useRouter()
const route = useRoute()
const headerStore = useHeaderStore()
const userStore = useUserStore()

const changeCollapse = () => {
  headerStore.changeCollapse()
}

const logout = () => {
  userStore.logout()
  router.push({
    path: '/login',
    query: {
      redirect: route.path
    }
  })
}
</script>

<template>
  <div class="header_container">
    <div class="left_container">
      <div class="colleps"></div>
      <div class="breadcrumd">
        <el-icon @click="changeCollapse"><Operation /></el-icon>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/' }">主页</el-breadcrumb-item>
          <el-breadcrumb-item>
            <a href="/">商品管理</a>
          </el-breadcrumb-item>
          <el-breadcrumb-item>SPU管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
    </div>
    <div class="right_container">
      <div class="right_username">{{ username }}</div>
      <div class="avtor">
        <el-dropdown trigger="click" type="success">
          <img src="../../assets/imgs/avtor.webp" />
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="logout">
                <span>退出登录</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.header_container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .left_container {
    font-size: 45px;
    margin: 0 15px;
    display: flex;
    justify-content: center;
    align-items: center;
    .breadcrumd {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 100%;
      height: 100%;
      .el-icon {
        width: 14px;
        height: 14px;
        margin-right: 5px;
      }
    }
  }
  .right_container {
    margin: 0 15px;
    display: flex;
    align-items: center;
    justify-content: center;
    .right_username {
      width: auto;
      font-size: 15px;
      line-height: 0.6em;
      font-weight: 300;
    }
    .avtor {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
}
</style>
